package com.abu.crms.controller;

import com.abu.crms.config.LoginInterceptor;
import com.abu.crms.mapper.UserMapper;
import com.abu.crms.vo.UserVO;
import com.abu.common.Resp;
import com.abu.crms.pojo.User;
import com.abu.crms.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * <p>
 * 普通用户表 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2021-02-03
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("noAuthorize/email/{email}")
    public Resp sendCodeToEmail(@PathVariable("email") String email) {
        Boolean flag = this.userService.sendCode(email);
        if (flag) {
            return Resp.ok().msg("发送验证码成功，5分钟内有效");
        }
        return Resp.error().msg("发送失败");
    }

    @GetMapping("noAuthorize/phone/{phone}")
    public Resp sendCodeToPhone(@PathVariable("phone") String phone) {
        Boolean flag = this.userService.sendCodeToPhone(phone);
        if (flag) {
            return Resp.ok().msg("发送成功，5分钟内有效");
        }
        return Resp.error().msg("发送失败");
    }

    @PostMapping("noAuthorize/loginUser/{phone}/{code}")
    public Resp loginUser(@PathVariable("phone") String phone, @PathVariable("code") String code) {
        String token = this.userService.loginUser(phone, code);
        if (StringUtils.isNotBlank(token)) {
            return Resp.ok().data("token", token);
        }
        return Resp.error().msg("登录失败");
    }

    @GetMapping("noAuthorize/openId/{openId}")
    public Resp getOneUserByOpenId(@PathVariable("openId") String openId) {
        User user = this.userService.getOne(new QueryWrapper<User>().eq("open_id", openId).select("user_id"));
        if (user == null) {
            return Resp.error().msg("数据为空").code(404);
        }
        return Resp.ok().data("user", user);
    }

    @GetMapping("/userId")
    public Resp getOneUserByUserId() {
        UserVO userVO = this.userService.queryUserInfoDetails();

        if (userVO == null) {
            return Resp.error().msg("数据为空").code(404);
        }
        return Resp.ok().data("user", userVO);
    }

    @GetMapping("/avatar")
    public Resp getUserAvatar() {
        Long userId = LoginInterceptor.getUserInfo().getUserId();
        User user = this.userService.getOne(new QueryWrapper<User>().eq("user_id", userId).select("img"));
        if (user == null) {
            return Resp.error().msg("数据为空").code(404);
        }
        return Resp.ok().data("user", user);
    }

    @PostMapping("noAuthorize/entity/{code}")
    public Resp saveUserInfo(@Valid @RequestBody User user, @PathVariable("code") String code) {
        Boolean flag = this.userService.saveUser(user, code);
        if (flag) {
            return Resp.ok().msg("保存成功");
        }
        return Resp.error().msg("保存失败");
    }

    @PostMapping("noAuthorize")
    public Resp queryUserInfoByCardNumAndPassword(
            @RequestParam("cardNum") String cardNum,
            @RequestParam("password") String password) {
        User user = this.userService.queryUserInfo(cardNum, password);
        return Resp.ok().data("user", user);
    }

    @PostMapping("entity/non")
    public Long saveUser(@RequestBody User user) {
        boolean flag = this.userService.save(user);
        if (flag) {
            return user.getUserId();
        }
        return null;
    }

    @PostMapping("/avatar/upload")
    public Resp uploadAvatar(@RequestParam("fileName") MultipartFile avatar) {
        Boolean flag = this.userService.uploadAvator(avatar);
        if (flag) {
            return Resp.ok().msg("更新成功");
        }
        return Resp.error().msg("更新失败");
    }

    @PutMapping("userEntity")
    public Resp updateUser(User user, @RequestParam("password") String password) {
        Boolean flag = this.userService.updateUser(user, password);
        if (flag) {
            return Resp.ok().msg("修改成功");
        }
        return Resp.error().msg("修改失败");
    }

    @PutMapping("noAuthorize/code")
    public Resp sendCodeToEmailOrPhone(@RequestParam("type") String type, @RequestParam("num") String sendTo) {
        Boolean flag = this.userService.sendCodeToEmailOrPhone(type, sendTo);
        if (flag) {
            return Resp.ok().msg("发送成功");
        }
        return Resp.error().msg("发送失败");
    }

    @PostMapping("noAuthorize/reset_pwd")
    public Resp setNewPwd(@RequestParam("code") String code, @RequestParam("num") String sendTo,@RequestParam("newPwd")String pwd) {
        Boolean flag = this.userService.resetPwd(code, sendTo,pwd);
        if (flag) {
            return Resp.ok().msg("重置成功");
        }
        return Resp.error().msg("重置失败");
    }


}

